package com.example.web.websystem.util;

import org.dozer.DozerBeanMapper;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

/**
 * Dozer工具类
 */
public class DozerUtil {
    private static DozerBeanMapper mapper;

    static {
        mapper = new DozerBeanMapper();
        mapper.setMappingFiles(Arrays.asList(
                "dozer/dozer-mapping-system.xml"
        ));
    }


    public static <T> T map(Object source, Class<T> destinationClass) {
        return mapper.map(source, destinationClass);
    }

    public static void map(Object source, Object destination) {
        mapper.map(source, destination);
    }

    public static <T> List<T> mapList(Collection sourceList, Class<T> destinationClass) {
        if (sourceList == null) return null;
        List<T> list = new ArrayList<>();
        sourceList.forEach(x -> list.add(mapper.map(x, destinationClass)));
        return list;
    }
}